$(document).ready(function () {


    //点击返回
    $(".go-back").click(function () {
        var url = $(this).attr("url");
        console.log(url);
        window.location = url;
    });

    //点击增加数量
    $(".add-wrap").click(function () {

        var id = $(this).data("id");

        var type = $(this).attr("type");
        console.log("type", type);

        var buyway = $(this).data("buyway");
        console.log("buyway", buyway);

        var current_product_num = $("#current_" + type + "_product_num").val();
        var product_num = $("#" + type + "_product_num").val();
        //购买方式是特定的数量
        if (buyway == 1) {
            if (current_product_num >= product_num) {
                return false;
            }
        }

        var num = parseInt($("#number_" + id).html());
        num++;
        console.log("num", num);
        $("#number_" + id).html(num);
        $("#product_item_" + id).addClass("active");

        var product_num_total = 0;
        var order_amount_total = 0;

        $(".number[type=" + type + "]").each(function () {
            var num = $(this).html();
            var price = parseFloat($(this).data("price"));
            product_num_total += parseInt(num);
            order_amount_total += parseFloat(num * price);
        });
        console.log("product_num_total", product_num_total);
        console.log("order_amount_total", order_amount_total);
        console.log($("#selected_" + type + "_num"));
        $("#selected_" + type + "_num").text(product_num_total);
        $("#current_" + type + "_product_num").val(product_num_total);
        $("#current_" + type + "_order_amount").val(order_amount_total);
        $("#" + type + "_order_amount_lable").html(order_amount_total);

        //检测是否显示确定按钮
        checkShowConfirmBtn();
    })

    //减少数量
    $(".minus-wrap").click(function () {
        var id = $(this).data("id");
        var num = parseInt($("#number_" + id).html());
        if (num > 0) {
            num--;
        }
        console.log("num", num);
        $("#number_" + id).html(num);
        if (num > 0) {
            $("#product_item_" + id).addClass("active");
        } else {
            $("#product_item_" + id).removeClass("active");
        }

        var product_num_total = 0;
        var order_amount_total = 0;
        var type = $(this).attr("type");
        console.log("type", type);

        $(".number[type=" + type + "]").each(function () {
            var num = $(this).html();
            var price = parseFloat($(this).data("price"));
            product_num_total += parseInt(num);
            order_amount_total += parseFloat(num * price);
        });
        console.log("product_num_total", product_num_total);
        console.log("order_amount_total", order_amount_total);
        $("#selected_" + type + "_num").text(product_num_total);
        $("#current_" + type + "_product_num").val(product_num_total);
        $("#current_" + type + "_order_amount").val(order_amount_total);
        $("#" + type + "_order_amount_lable").html(order_amount_total);

        //检测是否显示确定按钮
        checkShowConfirmBtn();
    })

    //点击确定
    $(".commit-btn").click(function () {
        if ($(this).hasClass("active")) {
            var level = $("#level").val();
            var formData = new FormData();

            //推荐人选择的商品
            $(".number").each(function () {

                var type = $(this).attr("type");
                if (type == "parent_user") {
                    var number = parseInt($(this).html());
                    var goodsid = $(this).data("goodsid");
                    var price = $(this).data("price");
                    var goodstype = $(this).data("goodstype");
                    if (number > 0) {
                        var data = {
                            "goodsid": goodsid,
                            "number": number,
                            "goodstype": goodstype,
                            "price": price,
                        };
                        formData.append('parent_user[]', JSON.stringify(data));
                    }
                }

            });

            //店铺选择的商品
            $(".number").each(function () {
                var type = $(this).attr("type");
                if (type == "company_store") {
                    var number = parseInt($(this).html());
                    var goodsid = $(this).data("goodsid");
                    var price = $(this).data("price");
                    var goodstype = $(this).data("goodstype");
                    if (number > 0) {
                        var data = {
                            "goodsid": goodsid,
                            "number": number,
                            "goodstype": goodstype,
                            "price": price,
                        };
                        formData.append('company_store[]', JSON.stringify(data));
                    }
                }
            });

            //级别选择的商品
            $(".number").each(function () {
                var type = $(this).attr("type");
                if (type == "relation_line") {
                    var number = parseInt($(this).html());
                    var goodsid = $(this).data("goodsid");
                    var price = $(this).data("price");
                    var goodstype = $(this).data("goodstype");
                    if (number > 0) {
                        var data = {
                            "goodsid": goodsid,
                            "number": number,
                            "goodstype": goodstype,
                            "price": price,
                        };
                        formData.append('relation_line[]', JSON.stringify(data));
                    }
                }
            });

            formData.append('level', level);

            $.ajax({
                url: '/index/expert/orderAdd',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                responseType: 'json', // 设置responseType为'json'
                success: function (back_json) {
                    console.log(back_json);
                    if (back_json["code"] == 1) {
                        var level = back_json["data"]["level"];
                        window.location = '/index/expert/orderShow?level=' + level;
                    }else{
                          window.location =back_json["url"];
                    }
                }
            });
        }
    });

});

/**
 * 检测显示确定的按钮
 */
function checkShowConfirmBtn() {

    var level = $("#level").val();

    var current_parent_user_product_num = parseFloat($("#current_parent_user_product_num").val());
    var current_parent_user_order_amount = parseFloat($("#current_parent_user_order_amount").val());

    var current_company_store_product_num = parseFloat($("#current_company_store_product_num").val());
    var current_company_store_order_amount = parseFloat($("#current_company_store_order_amount").val());

    var current_relation_line_product_num = parseFloat($("#current_relation_line_product_num").val());
    var current_relation_line_order_amount = parseFloat($("#current_relation_line_order_amount").val());

    var order_amount = parseFloat($("#order_amount").val());

    var parent_user_product_num = parseFloat($("#parent_user_product_num").val());
    var parent_user_order_amount = parseFloat($("#parent_user_order_amount").val());
    var parent_user_buy_way = parseFloat($("#parent_user_buy_way").val());

    var company_store_product_num = parseFloat($("#company_store_product_num").val());
    var company_store_order_amount = parseFloat($("#company_store_order_amount").val());
    var company_store_buy_way = parseFloat($("#company_store_buy_way").val());


    var relation_line_product_num = parseFloat($("#relation_line_product_num").val());
    var relation_line_order_amount = parseFloat($("#relation_line_order_amount").val());
    var relation_line_buy_way = parseFloat($("#relation_line_buy_way").val());

    //推荐人部分进行判断
    var parent_user_num_checked = false;
    var parent_user_amount_checked = false;
    var parent_user_checked = false;

    if (parent_user_buy_way == 1) {
        //选择的商品数量相等才行
        if (current_parent_user_product_num == parent_user_product_num) {
            parent_user_num_checked = true;
        }
    }
    if (parent_user_buy_way == 2) {
        //选择的商品数量大于或等于
        if (current_parent_user_product_num >= parent_user_product_num) {
            parent_user_num_checked = true;
        }
    }

    //订单金额大于或等于
    if (current_parent_user_order_amount >= parent_user_order_amount) {
        parent_user_amount_checked = true;
    }

    if (level == 1) {
        if (parent_user_num_checked || parent_user_amount_checked) {
            parent_user_checked = true;
        }
    }
    if (level == 2) {
        if (parent_user_num_checked || parent_user_amount_checked) {
            parent_user_checked = true;
        }
    }
    if (level == 3) {
        if (parent_user_num_checked || parent_user_amount_checked) {
            parent_user_checked = true;
        }
    }
    
    console.log("parent_user_num_checked", parent_user_num_checked);
    console.log("parent_user_amount_checked", parent_user_amount_checked);

    //公司店铺部分进行判断
    var company_store_num_checked = false;
    var company_store_amount_checked = false;
    var company_store_checked = false;

    if (company_store_buy_way == 1) {
        //数量相等才行
        if (current_company_store_product_num == company_store_product_num) {
            company_store_num_checked = true;
        }
    }
    if (company_store_buy_way == 2) {
        //数量大于或等于
        if (current_company_store_product_num >= company_store_product_num) {
            company_store_num_checked = true;
        }
    }

    //订单金额大于或等于
    if (current_company_store_order_amount >= company_store_order_amount) {
        company_store_amount_checked = true;
    }

    if (level == 1) {
        if (company_store_num_checked) {
            company_store_checked = true;
        }
    }
    if (level == 2) {
        if (company_store_num_checked || company_store_amount_checked) {
            company_store_checked = true;
        }
    }
    if (level == 3) {
        if (company_store_num_checked || company_store_amount_checked) {
            company_store_checked = true;
        }
    }


    console.log("company_store_num_checked", company_store_num_checked);
    console.log("company_store_amount_checked", company_store_amount_checked);

    //级别部分进行判断
    var relation_line_num_checked = false;
    var relation_line_amount_checked = false;
    var relation_line_checked = false;

    if (relation_line_buy_way == 1) {
        //数量相等才行
        if (current_relation_line_product_num == relation_line_product_num) {
            relation_line_num_checked = true;
        }
    }
    if (relation_line_buy_way == 2) {
        //数量大于或等于
        if (current_relation_line_product_num >= relation_line_product_num) {
            relation_line_num_checked = true;
        }
    }

    //订单金额大于或等于
    if (current_relation_line_order_amount >= relation_line_order_amount) {
        relation_line_amount_checked = true;
    }

    if (level == 1) {
        if (relation_line_num_checked) {
            relation_line_checked = true;
        }
    }
    if (level == 2) {
        if (relation_line_num_checked || relation_line_amount_checked) {
            relation_line_checked = true;
        }
    }
    if (level == 3) {
        if (relation_line_num_checked || relation_line_amount_checked) {
            relation_line_checked = true;
        }
    }

    console.log("relation_line_num_checked", relation_line_num_checked);
    console.log("relation_line_amount_checked", relation_line_amount_checked);


    if (parent_user_checked && company_store_checked && relation_line_checked) {
        $(".commit-btn").addClass("active");
    } else {
        $(".commit-btn").removeClass("active");
    }

}